package com.example.day07_zhongji.fragment;


import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.day07_zhongji.R;
import com.example.day07_zhongji.adapter.VpFragmentAdapter;
import com.example.day07_zhongji.base.BaseFragment;
import com.example.day07_zhongji.bean.TreBean;
import com.example.day07_zhongji.presenter.TreePresenter;
import com.example.day07_zhongji.view.TreeView;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class WanFragment extends BaseFragment<TreePresenter> implements TreeView {
    @BindView(R.id.vp_wan)
    ViewPager mvp_wan;
    @BindView(R.id.tab_wan)
    TabLayout mtab_wan;
    private ArrayList<String> strings;
    private ArrayList<Fragment> fragments;

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
    }

    @Override
    protected void initPresenter() {
        mPresenter=new TreePresenter();
        mPresenter.getdata();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_wan;
    }

    @Override
    public void onSuccess(TreBean data) {
        List<TreBean.DataBean> data1 = data.getData();
        strings = new ArrayList<>();
        fragments = new ArrayList<>();
        Log.i("asda", "onSuccess: "+data);
        for (int i = 0; i < data1.size(); i++) {
            TreBean.DataBean dataBean = data1.get(i);
            String name = dataBean.getName();
            int id = dataBean.getId();
            strings.add(name);
            ListFragment listFragment = new ListFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("cid",id);
            listFragment.setArguments(bundle);
            fragments.add(listFragment);
        }
        VpFragmentAdapter vpFragmentAdapter = new VpFragmentAdapter(getChildFragmentManager(), fragments,strings);
        mvp_wan.setAdapter(vpFragmentAdapter);
        mtab_wan.setupWithViewPager(mvp_wan);
    }

    @Override
    public void onFali(String error) {
        Toast.makeText(getContext(),error,Toast.LENGTH_LONG).show();
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(getContext(),str,Toast.LENGTH_LONG).show();

    }
}
